package com.ruoyi.message.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.message.domain.Message;
import com.ruoyi.message.domain.MessageVo;
import com.ruoyi.message.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 消息处理Controller
 *
 * @author ruoyi
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/message/message")
public class MessageController extends BaseController {
    @Autowired
    private IMessageService messageService;

    /**
     * 查询消息处理列表
     */
    @PreAuthorize("@ss.hasPermi('message:message:list')")
    @GetMapping("/list")
    public AjaxResult list(Message message) {
        startPage();
        List<Message> list = messageService.selectMessageList(message);
//        返回list即可
        return AjaxResult.success(new MessageVo(list, list.size()));
    }

    /**
     * 查询消息处理列表
     */
    @PreAuthorize("@ss.hasPermi('message:message:list')")
    @GetMapping("/list4user")
    public AjaxResult list4user(Message message) {
        startPage();
        List<String> types = new ArrayList<>();
        types.add("warn");
        return AjaxResult.success(getTypeMessage(message, types));
    }

    /**
     * 查询消息处理列表
     */
    @PreAuthorize("@ss.hasPermi('message:message:list')")
    @GetMapping("/list4admin")
    public AjaxResult list4admin(Message message) {
        startPage();
        List<String> types = new ArrayList<>();
        types.add("enter");
        types.add("fix");
        return AjaxResult.success(getTypeMessage(message, types));
    }

    /**
     * 导出消息处理列表
     */
    @PreAuthorize("@ss.hasPermi('message:message:export')")
    @Log(title = "消息处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Message message) {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        util.exportExcel(response, list, "消息处理数据");
    }

    /**
     * 获取消息处理详细信息
     */
    @PreAuthorize("@ss.hasPermi('message:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(messageService.selectMessageById(id));
    }

    /**
     * 新增消息处理
     */
    @PreAuthorize("@ss.hasPermi('message:message:add')")
    @Log(title = "消息处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Message message) {
        return toAjax(messageService.insertMessage(message));
    }

    /**
     * 修改消息处理
     */
    @PreAuthorize("@ss.hasPermi('message:message:edit')")
    @Log(title = "消息处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Message message) {
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除消息处理
     */
    @PreAuthorize("@ss.hasPermi('message:message:remove')")
    @Log(title = "消息处理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(messageService.deleteMessageByIds(ids));
    }

    private MessageVo getTypeMessage(Message message, List<String> types) {
        List<Message> totalMessageList = new ArrayList<>();
//        获取每个type对应的message
        types.forEach(type -> {
            message.setType(type);
            totalMessageList.addAll(messageService.selectMessageList(message));
        });
//        按sendTime逆序
        totalMessageList.sort(Comparator.comparing(Message::getSendTime).reversed());
        return new MessageVo(totalMessageList, totalMessageList.size());
    }

}
